package com.sdkx.app.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.app.Fragment;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import android.support.annotation.Nullable;

import com.sdkx.app.R;
import com.sdkx.app.ViewModel.WayPointManager;
import com.sdkx.app.activitys.SetCurrentAirportActivity;
import com.sdkx.app.activitys.SettingActivity;
import com.sdkx.app.model.WayPoint;
import com.sdkx.app.preference.ColorPickerPreference;

/**
 * A simple {@link Fragment} subclass.
 */
public class SettingFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
    SharedPreferences sharedPreferences;

    public SettingFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_settings);
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
    }

    private void init(){
        Preference preference = findPreference("benJiJiChang");
        Intent intent = new Intent(getActivity(), SetCurrentAirportActivity.class);
        preference.setIntent(intent);
        int current_airport_id = sharedPreferences.getInt("benJiJiChang", -1);
        if(current_airport_id !=-1){
            WayPoint wayPoint = WayPointManager.getItem(current_airport_id);
            if(wayPoint!=null){
                preference.setSummary(wayPoint.getName());
            }
        }

        Preference preference1 = findPreference("feiXingAnQuanCanShu");
        Intent intent2 = new Intent(getActivity(), SettingActivity.class);
        intent2.putExtra("type","security");
        preference1.setIntent(intent2);

        Preference preference2 = findPreference("daoHangXiangQing");
        Intent intent3 = new Intent(getActivity(), SettingActivity.class);
        intent3.putExtra("type","daohang");
        preference2.setIntent(intent3);

        Preference preference3 = findPreference("hangJiDianYanSe");
        preference3.setOnPreferenceClickListener(new ColorPickerPreference(getActivity()));

        ListPreference preference8 = (ListPreference)findPreference("diTuFangXiang");
        String diTuFangXiang = sharedPreferences.getString("diTuFangXiang","");
        preference8.setSummary(diTuFangXiang);

        SwitchPreference preference9 = (SwitchPreference) findPreference("genZongMoShi");
        boolean genZongMoShi = sharedPreferences.getBoolean("genZongMoShi",false);
        preference9.setSummary(genZongMoShi?"开":"关");

        SwitchPreference preference10 = (SwitchPreference) findPreference("jinLiChang");
        boolean jinLiChang = sharedPreferences.getBoolean("jinLiChang",false);
        preference10.setSummary(jinLiChang?"开":"关");

        SwitchPreference preference11 = (SwitchPreference) findPreference("dengJuYuan");
        boolean dengJuYuan = sharedPreferences.getBoolean("dengJuYuan",false);
        preference11.setSummary(dengJuYuan?"开":"关");

        SwitchPreference preference12 = (SwitchPreference) findPreference("luoPan");
        boolean luoPan = sharedPreferences.getBoolean("luoPan",false);
        preference12.setSummary(luoPan?"开":"关");

        SwitchPreference preference13 = (SwitchPreference) findPreference("zhiNanZhen");
        boolean zhiNanZhen = sharedPreferences.getBoolean("zhiNanZhen",false);
        preference13.setSummary(zhiNanZhen?"开":"关");

        SwitchPreference preference14 = (SwitchPreference) findPreference("qianJingFangXiangXian");
        boolean qianJingFangXiangXian = sharedPreferences.getBoolean("qianJingFangXiangXian",false);
        preference14.setSummary(qianJingFangXiangXian?"开":"关");

        SwitchPreference preference15 = (SwitchPreference) findPreference("biLiChi");
        boolean biLiChi = sharedPreferences.getBoolean("biLiChi",false);
        preference15.setSummary(biLiChi?"开":"关");

        ListPreference preference16 = (ListPreference)findPreference("zuoBiaoGeShi");
        String zuoBiaoGeShi = sharedPreferences.getString("zuoBiaoGeShi","");
        preference16.setSummary(zuoBiaoGeShi);

        ListPreference preference17 = (ListPreference)findPreference("hangJiXianShiDianShu");
        String hangJiXianShiDianShu = sharedPreferences.getString("hangJiXianShiDianShu","");
        preference17.setSummary(hangJiXianShiDianShu);
    }

    @Override
    public void onStart() {
        super.onStart();
        init();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
        switch (s) {
            case "benJiJiChang":
                WayPoint wayPoint = WayPointManager.getItem(sharedPreferences.getInt("benJiJiChang",-1));
                findPreference("benJiJiChang").setSummary(wayPoint==null?"":wayPoint.getName());
                break;
            case "diTuFangXiang":
                String diTuFangXiang = sharedPreferences.getString("diTuFangXiang","");
                findPreference("diTuFangXiang").setSummary(diTuFangXiang);
                break;
            case "genZongMoShi":
                boolean genZongMoShi = sharedPreferences.getBoolean("genZongMoShi",false);
                findPreference("genZongMoShi").setSummary(genZongMoShi?"开":"关");
                break;
            case "jinLiChang":
                boolean jinLiChang = sharedPreferences.getBoolean("jinLiChang", false);
                findPreference("jinLiChang").setSummary(jinLiChang?"开":"关");
                break;
            case "dengJuYuan":
                boolean dengJuYuan = sharedPreferences.getBoolean("dengJuYuan",false);
                findPreference("dengJuYuan").setSummary(dengJuYuan?"开":"关");
                break;
            case "luoPan":
                boolean luoPan = sharedPreferences.getBoolean("luoPan",false);
                findPreference("luoPan").setSummary(luoPan?"开":"关");
                break;
            case "zhiNanZhen":
                boolean zhiNanZhen = sharedPreferences.getBoolean("zhiNanZhen",false);
                findPreference("zhiNanZhen").setSummary(zhiNanZhen?"开":"关");
                break;
            case "qianJingFangXiangXian":
                boolean qianJingFangXiangXian = sharedPreferences.getBoolean("qianJingFangXiangXian",false);
                findPreference("qianJingFangXiangXian").setSummary(qianJingFangXiangXian?"开":"关");
                break;
            case "biLiChi":
                boolean biLiChi = sharedPreferences.getBoolean("biLiChi",false);
                findPreference("biLiChi").setSummary(biLiChi?"开":"关");
                break;
            case "zuoBiaoGeShi":
                String zuoBiaoGeShi = sharedPreferences.getString("zuoBiaoGeShi","");
                findPreference("zuoBiaoGeShi").setSummary(zuoBiaoGeShi);
                break;
            case "hangJiXianShiDianShu":
                String hangJiXianShiDianShu = sharedPreferences.getString("hangJiXianShiDianShu","");
                findPreference("hangJiXianShiDianShu").setSummary(hangJiXianShiDianShu);
                break;
            default:
                break;
        }
    }
}
